<template>
    <div class="login-container">
        <el-card class="box-card">
            <div class="login-body">
                <div class="login-title" @click="toIndex">
                    <img src="../../assets/logo.png" style="width: 45px; position: relative; top: 13px; right: 6px">
                    <span style="color: #00bcd4">骑手注册</span>
                </div>
                <el-form ref="form" :model="userForm" :rules="rules">
                    <el-form-item prop="riderName">
                        <el-input
                            placeholder="请输入骑手用户名..."
                            v-model="userForm.riderName"
                            class="login-input"
                        >
                            <template slot="prepend">
                                <div class="el-icon-user"></div>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="nickname">
                        <el-input
                            placeholder="请输入昵称..."
                            v-model="userForm.nickname"
                            class="login-input"
                        >
                            <template slot="prepend">
                                <div class="el-icon-user-solid"></div>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="phone">
                        <el-input
                            placeholder="请输入手机号..."
                            v-model="userForm.phone"
                            class="login-input"
                        >
                            <template slot="prepend">
                                <div class="el-icon-phone"></div>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input
                            placeholder="请输入密码..."
                            v-model="userForm.password"
                            class="login-input"
                            show-password
                        >
                            <template slot="prepend">
                                <div class="el-icon-lock"></div>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="confirmPassword">
                        <el-input
                            placeholder="请确认密码..."
                            v-model="userForm.confirmPassword"
                            class="login-input"
                            @keyup.enter.native="register"
                            show-password
                        >
                            <template slot="prepend">
                                <div class="el-icon-lock"></div>
                            </template>
                        </el-input>
                    </el-form-item>
                    <div class="login-submit">
                        <el-button type="primary" @click="register" class="login-btn">注册</el-button>
                        <el-button type="success" @click="$router.push('/login-rider')" class="login-btn">已有账号</el-button>
                    </div>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "register-rider",
    data() {
        const validateConfirmPassword = (rule, value, callback) => {
            if (value !== this.userForm.password) {
                callback(new Error('两次输入密码不一致'));
            } else {
                callback();
            }
        };
        
        return {
            userForm: {
                riderName: '',
                nickname: '',
                phone: '',
                password: '',
                confirmPassword: ''
            },
            rules: {
                riderName: [
                    { required: true, message: '请输入骑手用户名', trigger: 'blur' },
                    { min: 2, max: 10, message: '姓名长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                nickname: [
                    { required: true, message: '请输入昵称', trigger: 'blur' },
                    { min: 2, max: 15, message: '昵称长度在 2 到 15 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, message: '请确认密码', trigger: 'blur' },
                    { validator: validateConfirmPassword, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        register() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    // 构造注册数据
                    const registerData = {
                        riderName: this.userForm.riderName,
                        nickname: this.userForm.nickname,
                        phone: this.userForm.phone,
                        password: this.userForm.password
                    };

                    this.$api.riderRegister(registerData)
                        .then(res => {
                            console.log("骑手注册返回：", res);
                            if (res.code === 200) {
                                this.$message.success('注册成功，请登录');
                                this.$router.push('/login-rider');
                            } else {
                                this.$message.error(res.message || '注册失败');
                            }
                        })
                        .catch(err => {
                            console.error("注册失败:", err);
                            this.$message.error("注册失败，请稍后重试");
                        });
                } else {
                    this.$message.error('请填写完整信息');
                    return false;
                }
            });
        },
        toIndex() {
            this.$router.replace({ path: '/index' });
        }
    }
};
</script>

<style scoped>
    .login-container {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        width: 100%;
        background: url("../../assets/login-back.png") center top / cover no-repeat;
    }
    .login-body {
        padding: 40px;
        width: 400px;
        height: 100%;
        opacity: 0.95;
        font-size: 30px; /* 整体字体稍微大一点 */
    }
    .login-title {
        padding-bottom: 30px;
        text-align: center;
        font-weight: 600;
        font-size: 26px;
        color: #409EFF;
        cursor: pointer;
    }
    .login-input {
        margin-bottom: 20px;
    }
    .login-submit {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        gap: 15px;
    }
    .login-btn {
        width: 200px;
        height: 40px;
    }
    .sign-in-container {
        padding: 0 10px;
    }
    .sign-in-text {
        color: #409EFF;
        font-size: 16px;
        text-decoration: none;
        line-height:28px;
    }
    .other-submit{
        display:flex;
        justify-content: space-between;
        margin-top: 10px;
        margin-left: 0px;
    }
</style> 